package sk.stu.fei.tomsteam;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JOptionPane;

import org.snmp4j.smi.OID;

public class Agent {
	private String name;
	private String ipAddress;
	private String port;
	private String community;
	
	private String defaultOid;
	private String combinedIpPort;
	
	/*
	 * listOfAgents sluzi na ukladanie vsetkych instancii Agentov
	 * pri ich vytvarani cez AddAgentDialog a TreeInPanel
	 * a nasledne volanie metody na pristup k SNMP datam
	 * a informaciam o jednotlivych agentoch na danych
	 * adresach a portoch
	*/
	public static List<Agent> listOfAgents = new LinkedList<Agent>();
	
	public Agent(String name, String ipAddress, String port, String community) {
		this.name = name;
		this.ipAddress = ipAddress;
		this.port = port;
		this.community = community;
		this.defaultOid = ".1.3.6.1.2.1.1.3.0";	//sysUptime OID
		this.combinedIpPort = "udp:" + ipAddress + "/" + port;

	}
	
	/**
	 * this overriden method returns string which is used in tree
	 * as a nameplate for created Agent object
	 */
	@Override
	public String toString() {	//this string is used to name tree components
		return name;
	}
	
	
	public String getName() {
		return name;
	}
	
	
	public void setName(String name) {
		this.name = name;
	}
	
	
	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}
	
	public String getCommunity() {
		return community;
	}
	
	public void setCommunity(String community) {
		this.community = community;
	}

	public String executorMethod() {
    	try {
			SimpleSnmpClient client = new SimpleSnmpClient(combinedIpPort);
			//String createdResponse = client.getAsString(new OID(defaultOid));		//doplnena nula na konci - scalar object
			String createdResponse = client.processResponse(new OID(defaultOid), community);
			client.stop();
			return createdResponse;
    	}
    	catch (IOException ioe) {
    		//return "error" + ioe.getMessage();
    		return "IOException";
    	}
    	/*catch(NullPointerException npe) {
    		return "Unreachable";
    	}
		catch(ArrayIndexOutOfBoundsException e) {
			return "OutOfArrayBounds";
		}*/
	}

}
